﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.ComponentModel;
using AppSerwis.Model;
using AppSerwis.CustomCommand;
using System.Windows.Input;
using AppSerwis.Helpers;
using System.Windows.Controls;
using AppSerwis.View;

namespace AppSerwis.ViewModel
{
    public class LoginViewModel : ViewModelBase 
    {
        public LoginViewModel()
        {
            RequestClose += ShowMainView;
        }

        public event EventHandler RequestClose;

        #region Property

        private User _currentUser = new User();
        public User CurrentUser
        {
            get { return this._currentUser; }
            set 
            {
                this._currentUser = value;
                NotifyPropertyChanged("CurrentUser");
            }
        }        

        private string _error;
        public string Error
        {
            get { return this._error; }
            set
            {
                this._error = value;
                NotifyPropertyChanged("Error");
            }
        }
        

        #endregion

        private void ShowMainView(object sender, EventArgs e)
        {
            var mainViewModel = new MainViewModel(CurrentUser);            
            
            var mainView = new MainView();
            mainView.DataContext = mainViewModel;
            mainView.Show();
        }

        private void Login(object param)
        {
            var passwordBox = (PasswordBox)param;
            string hPassword = Helper.ComputeHash(passwordBox.Password + CurrentUser.LoginName, new SHA1CryptoServiceProvider());

            var model = new MainModelContext("MainConnectionString");
            var result = (from c in model.Users
                         where c.LoginName == CurrentUser.LoginName && 
                               c.Password == hPassword &&
                               c.IsActive == true
                          select c);
            
            foreach (var item in result)
            {
                CurrentUser = item;
                break;
            }

            if (CurrentUser.UserID == -1)
                this.Error = "Nie prawidłowa nazwa użytkownika lub hasło.";
            else
            {
                if (RequestClose != null)
                    RequestClose(this, new EventArgs());
            }
        }

        private bool CanLogin
        {
            get
            {
                if (String.IsNullOrEmpty(CurrentUser.LoginName))
                    return false;
                else
                    return true;
            }
        }

        #region Commands
        private ICommand _loginCommand;
        public ICommand LoginCommand
        {
            get
            {
                if (this._loginCommand == null)
                {
                    this._loginCommand = new RelayCommand(param => this.Login(param), param => this.CanLogin);
                }
                return this._loginCommand;
            }
        }
        #endregion
    }
}
